#!/bin/sh
##<!-- on mac
#gf=g95
GF=gfortran

FLAGS=
#flags=-g -fbounds-check -Wall -ftrace=full
#flags=-g -fbounds-check -Wall -fbacktrace -finit-real=nan -ffpe-trap=overflow
#flags=-g -fbounds-check -Wall -fbacktrace -finit-real=nan -ffpe-trap=overflow
#flags=-ggdb -fbacktrace

FPP=../fpp_ptc
PTC=../fpp_ptc
main=../example_programs
EXEDIR=example_executables

cat intro.txt

cd example_programs
ls
cd ..
cat blank.txt

# :start
#set /p mainprogram=  Write main program name =
read -p "Write main program name :" mainprogram

cat blank.txt
if [ "$mainprogram" = '' ]; then
   echo "invalid"
   exit 1
fi


compile_file()
{
   echo "Compiling `basename $1`"
   $GF $FLAGS -c $1
}

compile_all()
{
   rm -f *.mod
   rm -f *.obj
   rm -f *.o

   compile_file $FPP/a_scratch_size.f90
   compile_file $FPP/b_da_arrays_all.f90
   compile_file $FPP/c_dabnew.f90
   compile_file $FPP/d_lielib.f90
   compile_file $FPP/h_definition.f90
   compile_file $FPP/i_tpsa.f90
   compile_file $FPP/j_tpsalie.f90
   compile_file $FPP/k_tpsalie_analysis.f90
   compile_file $FPP/l_complex_taylor.f90
   compile_file $FPP/m_real_polymorph.f90
   compile_file $FPP/n_complex_polymorph.f90
   compile_file $FPP/o_tree_element.f90
   compile_file $PTC/Sa_extend_poly.f90
   compile_file $PTC/Sb_sagan_pol_arbitrary.f90
   compile_file $PTC/Sc_euclidean.f90
   compile_file $PTC/Sd_frame.f90
   compile_file $PTC/Se_status.f90
   compile_file $PTC/Sf_def_all_kinds.f90
   compile_file $PTC/Sg_sagan_wiggler.f90
   compile_file $PTC/Sh_def_kind.f90
   compile_file $PTC/Si_def_element.f90
   compile_file $PTC/Sk_link_list.f90
   compile_file $PTC/Sl_family.f90
   compile_file $PTC/Sm_tracking.f90
   compile_file $PTC/Sma0_beam_beam_ptc.f90
   compile_file $PTC/Sma_multiparticle.f90
   compile_file $PTC/Sn_mad_like.f90
   compile_file $PTC/So_fitting.f90
   compile_file $PTC/Sp_keywords.f90
   compile_file $PTC/Spb_fake_gino_sub.f90
   compile_file $PTC/Sq_orbit_ptc.f90
   compile_file $PTC/Sr_spin.f90
   compile_file $PTC/Sra_fitting.f90
   compile_file $PTC/Ss_fake_mad.f90
   compile_file $PTC/St_pointers.f90
   compile_file $PTC/zzy_run_madx.f90
}

compile_main()
{
   compile_file $main/$mainprogram.f90
}

link_program()
{
   COMMON_OBJS="St_pointers.o \
   a_scratch_size.o b_da_arrays_all.o c_dabnew.o \
   d_lielib.o h_definition.o \
   i_tpsa.o j_tpsalie.o k_tpsalie_analysis.o l_complex_taylor.o m_real_polymorph.o \
   n_complex_polymorph.o o_tree_element.o zzy_run_madx.o \
   Sa_extend_poly.o Sb_sagan_pol_arbitrary.o Sc_euclidean.o Sd_frame.o Se_status.o Sra_fitting.o \
   Sf_def_all_kinds.o Sg_sagan_wiggler.o Sh_def_kind.o Si_def_element.o Spb_fake_gino_sub.o \
   Sk_link_list.o Sl_family.o Sm_tracking.o Sma_multiparticle.o Sma0_beam_beam_ptc.o \
   Sn_mad_like.o So_fitting.o Sp_keywords.o Sr_spin.o Ss_fake_mad.o Sq_orbit_ptc.o"

   cd ..
   #echo "PWD=$PWD"
   echo "mkdir $EXEDIR/$mainprogram"
   mkdir $EXEDIR/$mainprogram
   #@COPY /Y terminal.lnk %out%%mainprogram%\terminal.lnk
   cd object_files

   echo "$GF $FLAGS -o ../$EXEDIR/$mainprogram/$mainprogram  $COMMON_OBJS $mainprogram.o"
   $GF $FLAGS -o ../$EXEDIR/$mainprogram/$mainprogram  $COMMON_OBJS $mainprogram.o
}


echo "Enter your choice : (full , main, link, carriage return to exit)"
read -p "So ? " choice
cd object_files

if [ "$choice" = "FULL" ] || [ "$choice" = "full" ]; then

   compile_all
   compile_main
   link_program

elif [ "$choice" = "MAIN" ] || [ "$choice" = "main" ]; then

   compile_main

elif [ "$choice" = "LINK" ] || [ "$choice" = "link" ]; then

   link_program

elif [ "$choice" = "" ]; then

   cd ..
   exit 0

fi

